package com.yikaiye.android.yikaiye.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umeng.socialize.weixin.view.WXCallbackActivity;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.model.EventBusModel;

import org.greenrobot.eventbus.EventBus;

public class WXEntryActivity extends WXCallbackActivity implements IWXAPIEventHandler {

    private IWXAPI api;
    private boolean isHas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_w_x_entry);
        api = WXAPIFactory.createWXAPI(this, Constant.WE_CHAT_APP_ID, false);
        api.handleIntent(getIntent(), this);
    }

    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {//判断小程序回调
            WXLaunchMiniProgram.Resp launchMiniProResp = (WXLaunchMiniProgram.Resp) baseResp;
            String extraData = launchMiniProResp.extMsg; //对应小程序组件 <button open-type="launchApp"> 中的 app-parameter 属性
        } else {
            /*
                {
                    "authResult":false,
                    "code":"031d1lml2Ii5R74ZTDol2yBHGE2d1lmn",
                    "country":"CN",
                    "lang":"zh_CN",
                    "state":"wechat_sdk_demo_test",
                    "url":"wx9af35e13ab141973://oauth?code\u003d031d1lml2Ii5R74ZTDol2yBHGE2d1lmn\u0026state\u003dwechat_sdk_demo_test",
                    "errCode":0
                }
             */
            SendAuth.Resp sp = (SendAuth.Resp) baseResp;
            if (!TextUtils.isEmpty(sp.code) && !isHas) {
                isHas = true;
                Log.e("返回信息", new Gson().toJson(baseResp));
                switch (baseResp.errCode) {
                    case BaseResp.ErrCode.ERR_OK:
                        EventBus.getDefault().post(new EventBusModel(sp.code));
                        break;
                    case BaseResp.ErrCode.ERR_USER_CANCEL:
                        break;
                    case BaseResp.ErrCode.ERR_AUTH_DENIED:
                        break;
                    case BaseResp.ErrCode.ERR_UNSUPPORT:
                        break;
                    default:
                        break;
                }
            }
        }
        finish();  //需要在小程序返回APP的时候，finish这个页面，否则其他页面无法点击
    }
}